/**
 * 业务异常类定义
 * 用于在 Service 层抛出具有明确语义的错误
 */

export class BusinessError extends Error {
    public code: number
    public statusCode: number

    constructor(message: string, code: number = 400, statusCode: number = 400) {
        super(message)
        this.name = 'BusinessError'
        this.code = code
        this.statusCode = statusCode
        Error.captureStackTrace(this, this.constructor)
    }
}

export class PermissionError extends BusinessError {
    constructor(message: string = 'Permission denied') {
        super(message, 403, 403)
        this.name = 'PermissionError'
    }
}

export class NotFoundError extends BusinessError {
    constructor(resource: string = 'Resource') {
        super(`${resource} not found`, 404, 404)
        this.name = 'NotFoundError'
    }
}

export class ValidationError extends BusinessError {
    public fields?: Record<string, string>

    constructor(message: string, fields?: Record<string, string>) {
        super(message, 422, 422)
        this.name = 'ValidationError'
        this.fields = fields
    }
}

export class ConflictError extends BusinessError {
    constructor(message: string = 'Resource conflict') {
        super(message, 409, 409)
        this.name = 'ConflictError'
    }
}

export class UnauthorizedError extends BusinessError {
    constructor(message: string = 'Unauthorized') {
        super(message, 401, 401)
        this.name = 'UnauthorizedError'
    }
}

export class CheckInError extends BusinessError {
    public failedConditions?: string[]

    constructor(message: string, failedConditions?: string[]) {
        super(message, 400, 400)
        this.name = 'CheckInError'
        this.failedConditions = failedConditions
    }
}
